package com.vanEngine.util
{
	import flash.net.getClassByAlias;
	import flash.net.registerClassAlias;
	import flash.utils.ByteArray;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;

	/**
	 * 对象操作的Util类 
	 * @author ZhongZw
	 * 
	 */	
	public class ObjectUtils
	{
		public function ObjectUtils()
		{
		}
		
		
		/**
		 * 可对简单对象的数据进行完全复制. 简单对象即指对象所有成员属性类型皆为基本类型( Boolean, int, Number, String ).
		 * 没有任何自定义类型.
		 * 
		 * @param obj
		 * @return 
		 * 
		 */		
		public function copySimpleObj( obj:Object ):Object
		{
			var className:String = getQualifiedClassName( obj );
			
			var defClass:Class = getDefinitionByName( className );
			
			var registeredClass:Class 
			
			try
			{
				registeredClass = getClassByAlias( className );
			}
			catch( e:Error )
			{
				
			}
			
			if( !registeredClass )
			{
				flash.net.registerClassAlias( className, defClass );
			}
			else if( registeredClass != defClass )
			{
				flash.net.registerClassAlias( className + "::" + defClass.toString(), defClass );
			}
			
			var byteArr:ByteArray = new ByteArray();
			
			byteArr.writeObject( obj );
			
			byteArr.position = 0;
			
			return byteArr.readObject();
			
		}
		
	}
}